package com.wisdytech.common.thrift.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wisdytech.common.enums.ActionType;
import com.wisdytech.common.thrift.api.DCSService;
import com.wisdytech.common.thrift.dto.ServerDto;
import com.wisdytech.common.thrift.service.IEquipmentInfoService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqClientDao;
import com.wisdytech.linkdcs.equipment.dao.IEqToClientDao;
import com.wisdytech.linkdcs.equipment.model.EquipmentToClient;

@Service
@Transactional
public class EquipmentInfoServiceImpl implements IEquipmentInfoService{
	
	private static Logger logger =LoggerFactory.getLogger(EquipmentInfoServiceImpl.class);
    
    @Resource
    private IEqClientDao clientDao;
    
    @Resource
    private IEqToClientDao eqToClientDao;

    
    /**
     * <p>Description: 调用dcs</p>
     * @param equipmentId 设备id
     * @param equipmentCode 设备编码
     * @param clientId 终端id
     * @param server 终端连接信息
     * @param type 推送状态
     * @return
     */
    private boolean sentToDCS(String equipmentId,String equipmentCode, String modelCode,String clientId, 
            ServerDto server,ActionType type){
        
        TTransport tTransport = null;
        try {
            tTransport = new TSocket(server.getIp(),server.getPort());
            tTransport.open();
            TProtocol protocol=new TBinaryProtocol(tTransport);
            DCSService.Client client =new DCSService.Client(protocol);
            client.putEquipmentInfo(equipmentId, modelCode, clientId, type.getCode(), System.currentTimeMillis(), equipmentCode);

            return true;
            
        } catch (TTransportException e) {
        	logger.error("推送设备信息失败 ",e);
            e.printStackTrace();
        } catch (TException e) {
        	logger.error("推送设备信息失败 ",e);
            e.printStackTrace();
        }finally{
            if(null != tTransport){
                tTransport.close();
            }
        }
        return false;
        
    }

    public boolean getEquipmentDetail(String eqCollectionId,ActionType methodType) {
    	
    	//查找相关对象
		EquipmentToClient etc=
				eqToClientDao.selectCollectionDetailById(eqCollectionId);
		
		ServerDto server = BaseUtil.getServerAddress(etc.getClientRoute());
        if(null == server){
            return false;
        }
        return sentToDCS(etc.getEquipId(), etc.getEquipCode(), etc.getModelCode(), etc.getClientId(), server, methodType);
        
    }
    

	@Override
	public boolean add(String eqCollectionId) {
		
        return getEquipmentDetail(eqCollectionId, ActionType.ADD);
	}

	@Override
	public boolean update(String eqCollectionId) {
		
		return getEquipmentDetail(eqCollectionId, ActionType.UPDATE);
	}

	@Override
	public boolean del(List<String> eqCollectionIds) {
		for(String eqCollectionId : eqCollectionIds) {
			if(!del(eqCollectionId)) {
				return false;
			}
		}
		return true;
	}
	
	public boolean del(String eqCollectionId) {
		return getEquipmentDetail(eqCollectionId, ActionType.DEL);
	}

	@Override
	public boolean updateByEquipment(String equipmentId) {
		
		//根据设备查找信息
		EntityWrapper<EquipmentToClient>  wrapper =new EntityWrapper<>();
		List<EquipmentToClient> etcs=
				eqToClientDao.selectList(wrapper.eq("EQUIP_ID", equipmentId));
		for(EquipmentToClient etc:etcs) {
			update(etc.getPkId());
		}
		
		return true;
	}

    
    
    

}
